﻿@using FujiyBlog.Web.Common
@model FujiyBlog.Web.Areas.Admin.ViewModels.AdminCommentIndex
@{
    ViewBag.Title = Resources.Labels.Comments;
    Layout = MVC.Admin.Comment.Views._Layout;
}
<h2>
    @Resources.Labels.Comments</h2>
@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)
<table>
    <tr>
        <th>
            <input type="checkbox" id="check-all-items" />
        </th>
        <th>
            @Html.LabelFor(x => x.Comments.FirstOrDefault().AuthorName)
        </th>
        <th>
            @Html.LabelFor(x => x.Comments.FirstOrDefault().AuthorEmail)
        </th>
        <th>
            @Html.LabelFor(x=>x.Comments.FirstOrDefault().AuthorWebsite)
        </th>
        <th>
            @Html.LabelFor(x=>x.Comments.FirstOrDefault().Comment)
        </th>
        <th>
            @Html.LabelFor(x => x.Comments.FirstOrDefault().IpAddress)
        </th>
        @*<th>
            @Html.LabelFor(x => x.Comments.FirstOrDefault().Avatar)
        </th>*@
        <th>
            @Html.LabelFor(x => x.Comments.FirstOrDefault().CreationDate)
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model.Comments)
    {
        <tr id="item-row-@item.Id">
            <td>
                <input type="checkbox" class="check-item" id="check-item@(item.Id)" value="@item.Id" />
            </td>
            <td>
                @if (item.Author == null)
                {
                    @Html.DisplayFor(modelItem => item.AuthorName)
                }
                else
                { 
                    @Html.DisplayFor(modelItem => item.Author.DisplayName)
                }
            </td>
            <td>
                @if (item.Author == null)
                {
                    @Html.DisplayFor(modelItem => item.AuthorEmail)
                }
                else
                {
                    @Html.DisplayFor(modelItem => item.Author.Email)
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AuthorWebsite)
            </td>
            <td>
                @Html.Raw(item.Comment.Substring(0, Math.Min(item.Comment.Length, 100)))
                @if (item.Comment.Length > 100)
                { <text>...</text>}
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IpAddress)
            </td>
@*            <td>
                @Html.DisplayFor(modelItem => item.Avatar)
            </td>*@
            <td>
                @DateTimeUtil.ConvertUtcToMyTimeZone(item.CreationDate)
            </td>
            <td>
                @Html.ActionLink("View", MVC.Post.Details(item.Post.Slug), null, null, null, "comment_id" + item.Id) |
                @Html.ActionLink("Edit", MVC.Admin.Comment.Edit(item.Id)) |
                @Ajax.ActionLink("Delete", MVC.Admin.Comment.Delete(item.Id), new AjaxOptions { HttpMethod = "POST", Confirm = "Sure?", OnSuccess = "RemoveItemRow(" + item.Id + ");" }, new { @class = "btn-delete-item" })
            </td>
        </tr>
    }
</table>
@if (ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString() == "Index")
{ 
    <a href="javascript:void(0);" id="approve-comments">Approve Selected</a>
}
else
{ 
    <a href="javascript:void(0);" id="disapprove-comments">Disapprove Selected</a>
}
<a href="javascript:void(0);" id="delete-comments">Delete Selected</a>
@Pager.Links(Model.CurrentPage, Model.TotalPages, Resources.Labels.NextPage, Resources.Labels.PreviousPage)
<script type="text/javascript">

    $('#check-all-items').click(function () {
        $('.check-item').attr('checked', this.checked)
    });

    function RemoveItemRow(itemId) {
        $('#item-row-' + itemId).hide();
    }

    function GetSelectedItems() {

        var values = new Array();

        $('.check-item:checked').each(function () {
            values.push($(this).val());
        });

        return values;
    }

    function SendCommentStatusChange(callUrl) {

        var ids = GetSelectedItems();

        var postData = JSON.stringify({ selectedComments: ids });

        $.ajax({
            type: "POST",
            contentType: 'application/json; charset=utf-8',
            url: callUrl,
            data: postData,
            success: function (response) {
                var len = ids.length;
                for (var i = 0; i < len; i++) {
                    RemoveItemRow(ids[i]);
                }
            },
            error: function () {
                alert('Error');
            }
        });
    }

    $('#approve-comments').click(function () {
        SendCommentStatusChange('@Url.Action(MVC.Admin.Comment.ApproveSelected())');
    });

    $('#disapprove-comments').click(function () {
        SendCommentStatusChange('@Url.Action(MVC.Admin.Comment.DisapproveSelected())');
    });

    $('#delete-comments').click(function () {
        SendCommentStatusChange('@Url.Action(MVC.Admin.Comment.DeleteSelected())');
    });

</script>
